package com.bangdao.juc;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author chen
 * @create 2021-01-03
 */
public class HashSetDemo {
    public static void main(String[] args) {
        //线程不安全
//        Set<String> set = new HashSet<>();
        //线程安全
//        Set<String> set = Collections.synchronizedSet(new HashSet<>());
        //线程安全
        Set<String> set = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(set);
            }).start();
        }
    }
}
